<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\member\controller;

/** 控制器类引入区 **/
use app\common\controller\Alipay;
use app\common\controller\Wxpay;
/** 模型类引入区 **/
use app\common\model\Cart as CartModel;
use app\common\model\ViperUser as ViperUserM;
use think\Db;

/** 核心或第三方类引入区 **/

/**
 * 购物车 控制器
 * @package app\api\controller
 */
class Cart extends Base
{
    public $noNeedLogin = [''];

    /**
     *   加入购物车
     **/
    public function addCart()
    {
        $user_id        = $this->user_id;
        $store_id       = $this->store_id;
        $cart_type      = $this->request->post('cart_type',0);   //购物车类型: 1=门店项目  2=门店商品
        $p_id           = $this->request->post('p_id',0);
        $num            = $this->request->post('num',1);
        $room_id            = $this->request->post('room_id',40);
        $num_type       = $this->request->post('num_type',1);      // 数量处理类型  1=累加   2=覆盖
        if(empty($cart_type)) {
            $this->result('请设置购物车类型','',204);
        }
        if(empty($p_id)) {
            $this->result('请选择商品','',204);
        }

        $data = Db::name('qt_room')
            ->field('id,name,status_id')
            ->where(['id' => $room_id, 'store_id' => $store_id])
            ->whereNotIn('status_id',[2,3,10])->find();
        if (!empty($data)) {
            $room_status = Db::name('qt_room_status')->where('store_id',$store_id)->column('name','id');
            $this->result('房间'.$room_status[$data['status_id']].'不能下单', '', 204);
        }

        $CartModel          = new CartModel();
        $result             = $CartModel->add($room_id,$user_id,$store_id,$p_id,$cart_type,$num,$num_type);
        if($result['status']){
            $this->result($result['msg'],$result['data'],200);
        }else{
            $this->result($result['msg'],$result['data'],204);
        }
    }


    /**
     *  购物车列表
    **/
    public function cartList()
    {
        $user_id        = $this->user_id;
        $store_id       = $this->store_id;

        $room_id            = $this->request->post('room_id',40);
        $ids            = $this->request->post('ids',"");
        $cart_type      = $this->request->post('cart_type');  //购物车类型: 1=门店项目  2=门店商品
        $CartModel      = new CartModel();
        $result = $CartModel->info($room_id,$user_id,$store_id,$cart_type, $ids);
        if($result['status']){
            $this->result($result['msg'],$result['data'],200);
        }else{
            $this->result($result['msg'],$result['data'],204);
        }
    }


    /**
     *  购物车结算页信息
     **/
    public function checkOut()
    {
        $store_id       = $this->store_id;
        $cart_type      = $this->request->post('cart_type');  //购物车类型: 1=门店项目  2=门店商品
        $ids            = $this->request->post('ids',"");
        $room_id            = $this->request->post('room_id',40);
        if(empty($ids))
        {
            $this->result('请选择结算的商品','',200);
        }

        $CartModel          = new CartModel();
        $result = $CartModel->outInfo($room_id,$this->user_id,$store_id,$cart_type,$ids);
        if($result['status']){
            $this->result('获取成功',$result['data'],200);
        }else{
            $this->result($result['msg'],'',204);
        }
    }


    /**
     *  设置购物车数量
    **/
    public function setNumsCart()
    {
        $store_id   = $this->store_id;
        $id         = $this->request->post('id',0);
        $num        = $this->request->post('num',1);
        $room_id        = $this->request->post('room_id',40);
        $CartModel          = new CartModel();
        $result = $CartModel->setNums($room_id,$this->user_id,$store_id, $id, $num);
        if($result){
            $this->result('更改成功','',200);
        }else{
            $this->result('更改失败','',204);
        }
    }

    /**
     *  删除购物车商品
    **/
    public function delCart()
    {
        $store_id   = $this->store_id;
        $ids        = $this->request->post('ids',"");
        $room_id        = $this->request->post('room_id',40);
        if(empty($ids)) $this->result('请选择要删除的商品','',204);
        $CartModel          = new CartModel();
        $result             = $CartModel->del($room_id,$this->user_id,$store_id,$ids);
        if($result){
            $this->result('删除成功','',200);
        }else{
            $this->result('删除失败','',204);
        }
    }
}
